#include "level1.h"

Level1::Level1(int health, int score) : Levels()
{
    player.setHealth(health);

    bgX1 = -60; // -60
    bgX2 = -130; // -110
    bgX3 = -240; // -
    bgY=0; // is this used anymore?

   // characterY=330;
    max_right_movement=500;
    max_left_movement=300;

     // More test
    jump_max = 110;
    player_current_y = 340;
    jump_spd = 4;
    player_current_floor_y = 340;
    //CONST_LEVEL1_MAIN_Y = new int( 340 );

    scrolling = true;
    scrolling_right = true; // this var is in levels

    level_alive = true;
    restart_level = false;

    player_level_x = 300;
    player_screen_x = 300;

    level_end_point = 1035 * 10;
    level_start_point = 0;

    // create enemys in the level
    build_enemys();

    txtOut = new MyText(25);
    high_score=0;

    // load level images
    load_images();

    // build the levels blocks
    build_level_blocks();

    // set the level music
    //music->create_audio_thread( "sounds/level1/level1music.ogg", "LF" );
}

Level1::~Level1()
{
    cout << "Destroyed level 1\n";
    delete txtOut;

    delete grass0;
    delete small_grass0;
    delete first_towers;
    delete second_towers;
    delete hole0;

}

void Level1::load_images()
{
    clouds.load( "grafx/level1/clouds.png" );
    mountain.load( "grafx/level1/mountain.png" );
    ground.load( "grafx/level1/ground.png" );
}

void Level1::build_level_blocks()
{
    small_grass0 = new Block( 0, 0, "grafx/level1/grassblocksmall.png", 'N' );
    grass0 =  new Block( 0, 0, "grafx/level1/grassblock.png", 'N' );
    first_towers = new Block( 0, 0, "grafx/level1/groundtower.png", 'A' );
    second_towers = new Block( 0, 0, "grafx/level1/groundtower.png", 'D' );
    hole0 = new Block( 0, 0, "grafx/level1/rockbg.png", 'N' );
}

void Level1::print_level_images( int bgY, int bgX1, int bgX2, int bgX3)
{
    clouds.draw( bgX1, bgY-445);
    clouds.draw( bgX1 + 985, bgY-445);
    clouds.draw( bgX1 + (985 * 2), bgY-445);

    mountain.draw( bgX2, bgY+165);
    mountain.draw( bgX2 + 999, bgY+165);
    mountain.draw( bgX2 + (999 * 2), bgY+165);
    mountain.draw( bgX2 + (999 * 3), bgY+165);
    mountain.draw( bgX2 + (999 * 4),bgY+165);
    mountain.draw( bgX2 + (999 * 5),bgY+165);

    // all the levels ground images are going to be printed all at once
}

void Level1::calc_screen_movement()
{
     // right movement
    if( player_screen_x > max_right_movement )
    {
//        cout << "player_level_x: " << player_level_x << endl;
//        cout << "level end point: " << level_end_point << endl;
        if( player_level_x > level_end_point )
        {
            cout << "End of level!1!!!\n";
            level_alive = false;
            player_level_x = level_end_point;
            player_screen_x = 500;
        }
        else
        {
            player_screen_x = max_right_movement;
            bgX1=bgX1-1; // 2
            bgX2=bgX2-2; // 5
            bgX3=bgX3-5; // 15
        }

    }

    // left movement
    if( player_screen_x < max_left_movement )
    {
        if( player_level_x < level_start_point )
        {
            //cout << "Start of level!!!1!\n";
            player_level_x = level_start_point;
            player_screen_x = 300;
        }
        else
        {
            player_screen_x = max_left_movement;
            bgX1=bgX1+1;
            bgX2=bgX2+2;
            bgX3=bgX3+5;
        }
    }

} // calc_screen_movement()

void Level1::detect_bullet_firing()
{
    // check if the player is dead
    if( true == player.Is_dead() )
    {
        Restart_level();
    }

    SDL_Rect player_pos = player.Get_sprite_info();
    if( true == player.Is_attacking() )
    {
        //music->create_audio_thread( "sounds/effects/Gun_Luger.wav", "LO" );
        Bullet *new_bullet =  new Bullet( 'P', player_pos.x, player_pos.y + 25, player.Is_moving_right() );
        bullets.Add_bullet( new_bullet );
        player.Set_attacking( false );
    }

    vector< Enemy >::iterator enemy_iter = enemys.Get_enemy_control_begin();
    while( enemy_iter != enemys.Get_enemy_control_end() )
    {
        if( true == (*(enemy_iter)).Is_attacking() )
        {
            //music->create_audio_thread( "sounds/effects/Gun_Luger.wav", "LO" );
            SDL_Rect enemy_pos = (*(enemy_iter)).Get_sprite_info();
            Bullet *new_bullet =  new Bullet( 'E', enemy_pos.x, (enemy_pos.y + 23), (*(enemy_iter)).Is_moving_right() );
            bullets.Add_bullet( new_bullet );
            (*(enemy_iter)).Set_attacking( false );
        }
        enemy_iter++;
    }
} // detect_bullet_firing()

void Level1::build_enemys()
{
    Enemy tmp( 3800, 360 );
    enemys.Add_enemy( tmp );

    tmp.Set_enemy( 8700, 360 );
    enemys.Add_enemy( tmp );

    tmp.Set_enemy( 4100, 360 );
    enemys.Add_enemy( tmp );


    tmp.Set_enemy( 5700, 360 );
    enemys.Add_enemy( tmp );

    tmp.Set_enemy( 3100, 360 );
    enemys.Add_enemy( tmp );


    tmp.Set_enemy( 9700, 360 );
    enemys.Add_enemy( tmp );

    tmp.Set_enemy( 5400, 360 );
    enemys.Add_enemy( tmp );


    tmp.Set_enemy( 7700, 360 );
    enemys.Add_enemy( tmp );

    tmp.Set_enemy( 7100, 360 );
    enemys.Add_enemy( tmp );
}

void Level1::Check_player_jumping()
{
    if( true == player.Is_jumping() )
    {
        player_current_y -= jump_spd;

        if( player_current_y <= (player_current_floor_y - jump_max) )
        {
            jump_spd = -jump_spd;
        }

        if( player_current_y >= 340 )
        {
            player.Set_jumping( false );
            Reset_jump_spd();
            player_current_floor_y = 340;
        }

    }
    else if( false == player.Is_jumping() )
    {
        if( false == player.Is_on_block() )
        {
            if( player_current_y < 340 )
            {
                player.Set_landed( false );
                player_current_y += jump_spd;
            }

        }

        if( player_current_y >= 340 )
        {
            player.Set_landed( true );
            Reset_jump_spd();
            player_current_floor_y = 340;
        }
    }

} // Check_player_jumping()

void Level1::draw()
{
    if(scrolling)
    {
        scrolling_right = player.Is_moving_right();
    }
    calc_screen_movement();

    print_level_images( bgY, bgX1, bgX2, bgX3 );

    // detect who is attacking
    detect_bullet_firing();

    Check_player_jumping();

    // Draw the player, enemys and bullets on screen;
    player.Draw( player_screen_x, player_current_y ); //player_current_y
    enemys.Draw( bgX3, bgY + 360, player_level_x, player_screen_x );
    bullets.Draw(enemys, player );

    // draws the ground objects on the level
    draw_ground_objects();


} // Draw()

void Level1::draw_ground_objects()
{

    for(int i=0;i<=level_end_point + 640; i=i+120) // 480
    {
        ground.draw( bgX3+i, bgY+425);
    }

    // first part
    small_grass0->Draw( bgX3 + 1000 , 320);
    small_grass0->Detect_collision( player, enemys, this, bullets );

    hole0->Draw( bgX3 + 900, 430 );
    hole0->Detect_collision( player, enemys, this, bullets );

    first_towers->Draw( bgX3 + 1200, 340 );
    first_towers->Detect_collision( player, enemys, this, bullets );

    hole0->Draw( bgX3 + 1950, 430 );
    hole0->Detect_collision( player, enemys, this, bullets );

    hole0->Draw( bgX3 + 2205, 430 );
    hole0->Detect_collision( player, enemys, this, bullets );

    grass0->Draw( bgX3 + 2150, 190);
    grass0->Detect_collision( player, enemys, this, bullets );

    second_towers->Draw( bgX3 + 2500, 340 );
    second_towers->Detect_collision( player, enemys, this, bullets );


    // second part
    hole0->Draw( bgX3 + 5000, 430 );
    hole0->Detect_collision( player, enemys, this, bullets );

    hole0->Draw( bgX3 + 5255, 430 );
    hole0->Detect_collision( player, enemys, this, bullets );

    hole0->Draw( bgX3 + 5510, 430 );
    hole0->Detect_collision( player, enemys, this, bullets );

    hole0->Draw( bgX3 + 5765, 430 );
    hole0->Detect_collision( player, enemys, this, bullets );

    small_grass0->Draw( bgX3 + 5100 , 320);
    small_grass0->Detect_collision( player, enemys, this, bullets );

    small_grass0->Draw( bgX3 + 5350 , 250);
    small_grass0->Detect_collision( player, enemys, this, bullets );

    small_grass0->Draw( bgX3 + 5560 , 320);
    small_grass0->Detect_collision( player, enemys, this, bullets );

    small_grass0->Draw( bgX3 + 5770 , 270);
    small_grass0->Detect_collision( player, enemys, this, bullets );


}//end draw_ground_objects function()

void Level1::moveleft()
{
   player.Set_moving_right( false );

    if( false == scrolling_right )
    {
        // move the player animations by one
        player.Move_frame();
        player_screen_x -= 5;
        player_level_x -= 5;
    }


}

void Level1::moveright()
{
    player.Set_moving_right( true );

    if( true == scrolling_right )
    {
        // move the player animations by one
        player.Move_frame();
        player_screen_x += 5;
        player_level_x += 5;
    }

}

void Level1::moveup()
{
    if( false == player.Is_jumping() && true == player.Landed() )
    {
        player.Set_landed( false );
        //music->create_audio_thread( "sounds/effects/coin.wav", "LO" );
        player.Set_jumping( true );
    }

}

void Level1::attack()
{

    player.Set_attacking( true );
}

void Level1::Set_player_main_floor_y( int main_floor_y )
{
    //player_bgY = 0;
    player_current_floor_y = main_floor_y;
}

void Level1::Reset_jump_spd()
{
    jump_spd = 4;
}


